home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Tools&Utilities / EnterAct Stuff / write your own Drag_on / CodeResource_Files.c < prev    next >
C/C++ Source or Header  |  1994-12-22  |  3KB  |  133 lines

  1. /* CodeResource_Files.c - read stdin, write stdout
  2. using Mac calls rather than stdio. */
  3.  
  4. /* stdin and stdout are just ordinary text files, the full path
  5. names for these files being supplied by the calling application
  6. in gacc.stdInFileName and gacc.stdOutFileName - gacc is a global
  7. copy of the AppCodeComm struct passed to the code resource by the
  8. application (see Code_Main.c). The full path name is converted to
  9. a regular file name and vRefNum, and then things proceed in a very
  10. standard way, like reading or saving using SFGetFile or SFPutFile.
  11. */
  12.  
  13. #include "CodeResource.h"
  14. #include "AppCodeComm.h"
  15. #include <string.h>
  16.  
  17. Handle ReadStdIn(void);
  18. Boolean PutToStdOut(Handle hText);
  19. short GetNameAndVRefNum(char *name, char *filename);
  20.  
  21. /* Read in the contents of stdin to a handle. Note if there is
  22. nothing in the file then NULL is returned rather than an empty
  23. handle. The full path name of stdin is in gacc.stdInFileName. */
  24. Handle ReadStdIn()
  25.     {
  26.     Handle        hText = NULL;
  27.     long        count;
  28.     short            saveVol, refNum, vRefNum;
  29.     char        filename[32];
  30.     Boolean        opened = FALSE;
  31.     
  32.     if (GetVol(NULL, &saveVol))
  33.         saveVol = 0;
  34.     if ((vRefNum = GetNameAndVRefNum(gacc.stdInFileName, filename)) == 0)
  35.         goto JumpOut;
  36.     
  37.     if (FSOpen((StringPtr)filename, vRefNum, &refNum))
  38.         goto JumpOut;
  39.     else
  40.         opened = TRUE;
  41.     if (GetEOF(refNum, &count) || count <= 0L)
  42.         goto JumpOut;
  43.     hText = NewHandle(count);
  44.     if (MemError() != noErr)
  45.         {
  46.         MemoryAlert();
  47.         goto JumpOut;
  48.         }
  49.     if (FSRead(refNum, &count, *hText))
  50.         {
  51.         DisposHandle(hText);
  52.         hText = NULL;
  53.         goto JumpOut;
  54.         }
  55. JumpOut:
  56.     if (opened)
  57.         FSClose(refNum);
  58.     if (saveVol)
  59.         SetVol(NULL, saveVol);
  60.     return(hText);
  61.     }
  62.  
  63.  
  64. /* Write the text in hText to stdout as specified by the full path name
  65. in gacc.stdOutFileName. */
  66. Boolean PutToStdOut(Handle hText)
  67.     {
  68.     long        count;
  69.     short            saveVol, refNum, vRefNum;
  70.     char        filename[32];
  71.     
  72.     if (GetVol(NULL, &saveVol))
  73.         saveVol = 0;
  74.     if ((vRefNum = GetNameAndVRefNum(gacc.stdOutFileName, filename)) == 0)
  75.         goto JumpOut;
  76.     
  77.     FSDelete((StringPtr)filename, vRefNum);
  78.     if (Create((StringPtr)filename, vRefNum, '????', 'TEXT'))
  79.         goto JumpOut;
  80.     if (FSOpen((StringPtr)filename, vRefNum, &refNum))
  81.         goto JumpOut;
  82.     count = GetHandleSize(hText);
  83.     if (FSWrite(refNum, &count, *hText))
  84.         {
  85.         FSClose(refNum);
  86.         goto JumpOut;
  87.         }
  88.     FSClose(refNum);
  89.     
  90.     FlushVol(NULL, vRefNum);
  91.     if (saveVol)
  92.         SetVol(NULL, saveVol);
  93.     return(TRUE);
  94.     
  95. JumpOut:
  96.     if (saveVol)
  97.         SetVol(NULL, saveVol);
  98.     return(FALSE);
  99.     }
  100.  
  101. /* Convert the full path name "name" into filename and vRefNum.
  102. "name" looks like "DiskName:folder1:folder2:...folderN:filename",
  103. so we back up from the end of "name" to the first colon to determine
  104. the filename. */
  105. short GetNameAndVRefNum(char *name, char *filename)
  106.     {
  107.     WDPBRec        theParms;
  108.     short         len = strlen(name);
  109.     Ptr            endPtr = name + len, startPtr = endPtr;
  110.     char        volname[256];
  111.     
  112.     while (startPtr >= name)
  113.         {
  114.         if (*startPtr == ':')
  115.             break;
  116.         --startPtr;
  117.         }
  118.     ++startPtr;
  119.     if (startPtr >= endPtr)
  120.         return(0);
  121.     filename[0] = endPtr - startPtr;
  122.     BlockMove(startPtr, filename+1, filename[0]);
  123.     volname[0] = startPtr - name;
  124.     BlockMove(name, volname+1, startPtr - name);
  125.     theParms.ioVRefNum = 0;
  126.     theParms.ioNamePtr = (StringPtr)volname;
  127.     theParms.ioWDDirID = 0;
  128.     theParms.ioWDProcID = 'ERIK';
  129.     if (PBOpenWD(&theParms, FALSE)) /* IM IV pg 158 */
  130.         return(0);
  131.     return(theParms.ioVRefNum);
  132.     }
  133.